#!/usr/bin/env python3
"""
人脸和车牌信息遮拦处理 Web 应用入口文件
"""
import uvicorn
import argparse
from pathlib import Path
import logging

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

def parse_args():
    """解析命令行参数"""
    parser = argparse.ArgumentParser(description="人脸和车牌信息遮拦处理 Web 应用")
    parser.add_argument(
        "--host",
        type=str,
        default="127.0.0.1",
        help="绑定主机地址 (默认: 127.0.0.1)"
    )
    parser.add_argument(
        "--port",
        type=int,
        default=8000,
        help="绑定端口号 (默认: 8000)"
    )
    parser.add_argument(
        "--reload",
        action="store_true",
        help="启用自动重载 (开发模式)"
    )
    return parser.parse_args()

def check_environment():
    """检查必要的环境配置"""
    required_dirs = [
        Path("static/uploads"),
        Path("static/processed"),
        Path("templates")
    ]
    
    for dir_path in required_dirs:
        if not dir_path.exists():
            dir_path.mkdir(parents=True, exist_ok=True)
            logger.info(f"创建目录: {dir_path}")

def main():
    args = parse_args()
    check_environment()
    
    uvicorn.run(
        "app.main:app",
        host=args.host,
        port=args.port,
        reload=args.reload,
        log_level="info",
        reload_dirs=["app"]
    )

if __name__ == "__main__":
    main()